<?php

namespace app\admin\controller;

use think\Controller;
use think\Request;
use think\Session;
use think\Db;
use think\View;
use think\Lang;

class Base extends Controller
{

    public function _initialize()
    {
        if (!Session::has('username','admin')){
            $this->redirect('admin/Login/index');
        }

        $this->getSystem();

    }


    //获取系统配置
    private function getSystem(){
        $data = Db::name('system')->select();
        foreach ($data as $k => $v) {
            View::share($v['en_name'], json_decode($v['config'], TRUE));
        }
    }



    // 根据附件路径删除附件
    public function delAccessory(Request $request)
    {
        if ($request->isGet()) {
            $data = $request->param();
            if (file_exists('.'.$data['accessory_path'])) {
                $ok = unlink('.'.$data['accessory_path']);
                if ($ok) {
                    $data = ['code'=>1,'msg'=>'删除成功'];
                }else{
                    $data = ['code'=>0,'msg'=>'删除失败'];
                }
            }else{
                $data = ['code'=>1,'msg'=>'附件已经不存在了'];
            }
            return $data;
        }
    }

    // 清除缓存
    public function doClear(Request $request)
    {
        $path = ROOT_PATH."/runtime/";
        if ($request->has('data')) {
            $data = $request->param();
        }else{
            $data = ['cache','log','temp','../public/uploads/temp'];
        }

        $count = count($data);

        for ($i = 0; $i < $count; $i++) {
            $this->dirDel($path . $data[$i] . "/");
        }
        return ['msg'=>'清除缓存成功'];
    }
    //删除目录
    protected function dirDel($path)
    {
        if (!is_dir($path)) {
            return ['msg'=>'缓存已经清理过了'];
        }
        $hand = opendir($path);
        while (($file = readdir($hand)) !== false) {
            if ($file == "." || $file == "..") {
                continue;
            }
            if (is_dir($path . "/" . $file)) {
                $this->dirDel($path . "/" . $file);
            } else {
                @unlink($path . "/" . $file);
            }
        }
        closedir($hand);
        @rmdir($path);
    }
    /*空操作时调用
     * */
    public function _empty(Request $request)
    {
        $this->error($request->action() . '该方法不存在!','Index/welcome');
    }
}
